package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.system.domain.Device;
import com.ruoyi.system.domain.DeviceDetail;
import com.ruoyi.system.domain.Alarm;
import com.ruoyi.system.domain.deviceAlarm.BpAlarm;
import com.ruoyi.system.domain.deviceAlarm.DeviceAlarm;
import com.ruoyi.system.domain.deviceAlarm.HrAlarm;
import com.ruoyi.system.domain.deviceAlarm.Spo2Alarm;
import com.ruoyi.system.mapper.DeviceMapper;
import com.ruoyi.system.mapper.AlarmMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Component;
import com.ruoyi.system.service.IAlarmService;

/**
 * 警报Service业务层处理
 * 
 * @author 22008021011kjp
 * @date 2025-06-23 14:41:55
 */
@Service
@Component
public class AlarmServiceImpl implements IAlarmService 
{
    @Autowired
    private AlarmMapper alarmMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询警报
     * 
     * @param sn 警报主键
     * @return 警报
     */
    @Override
    public Alarm selectAlarmBySn(String sn)
    {
        return alarmMapper.selectAlarmBySn(sn);
    }

    /**
     * 查询警报列表
     * 
     * @param alarm 警报
     * @return 警报
     */
    @Override
    public List<Alarm> selectAlarmList(Alarm alarm)
    {
        loadAlarmToDB();
        return alarmMapper.selectAlarmList(alarm);
    }
    @Override
    public boolean loadAlarmToDB(){

        List<Device> alldevices=deviceMapper.selectDeviceList(new Device());
        for(Device oneDevice : alldevices){
            String sn=oneDevice.getSn();
            DeviceDetail detail= getDetailFromCache(sn);
            List<DeviceAlarm> alarms=detail.getAlarms();
            insertAlarms(sn,alarms);
        }
        return true;
    }

    private DeviceDetail getDetailFromCache(String deviceId){
        DeviceDetail detail = new DeviceDetail(deviceId);
        String key=String.format("HEALTH_WEARABLE_ALARM_%s",deviceId);
        List<JSONObject> listData=redisCache.getCacheList(key);
        if(listData!=null && !listData.isEmpty()){
            detail.put("ALARM",listData);
        }
        return detail;
    }

    /**
     * 新增警报
     * 
     * @param alarm 警报
     * @return 结果
     */
    @Override
    public int insertAlarm(Alarm alarm)
    {
        return alarmMapper.insertAlarm(alarm);
    }

    /**
     * 修改警报
     * 
     * @param alarm 警报
     * @return 结果
     */
    @Override
    public int updateAlarm(Alarm alarm)
    {
        return alarmMapper.updateAlarm(alarm);
    }

    /**
     * 批量删除警报
     * 
     * @param sns 需要删除的警报主键
     * @return 结果
     */
    @Override
    public int deleteAlarmBySns(String[] sns)
    {
        return alarmMapper.deleteAlarmBySns(sns);
    }

    /**
     * 删除警报信息
     * 
     * @param sn 警报主键
     * @return 结果
     */
    @Override
    public int deleteAlarmBySn(String sn)
    {
        return alarmMapper.deleteAlarmBySn(sn);
    }

    @Override
    public void insertAlarms(String sn, List<DeviceAlarm> alarms) {
        if (alarms != null && !alarms.isEmpty()) {
            for (DeviceAlarm one : alarms) {
                List<BpAlarm> bpAlarmList = one.getAlarmBps();
                if (bpAlarmList != null && !bpAlarmList.isEmpty()) {
                    for (BpAlarm bpOne : bpAlarmList) {
                        try {
                            Alarm alarm = new Alarm();
                            alarm.setName("DBP");
                            alarm.setValue(BigDecimal.valueOf(bpOne.getDbp()));
                            alarm.setSn(sn);
                            alarm.setTimeStamp(bpOne.getTimeStamp());
                            alarmMapper.insertAlarm(alarm);
                            alarm.setName("SBP");
                            alarm.setValue(BigDecimal.valueOf(bpOne.getSbp()));
                            alarmMapper.insertAlarm(alarm);
                        } catch (DuplicateKeyException ignored) {
                        }
                    }
                }

                List<HrAlarm> hrAlarmList = one.getAlarmHrs();
                if (hrAlarmList != null && !hrAlarmList.isEmpty()) {
                    for (HrAlarm hrOne : hrAlarmList) {
                        Alarm iotAlarm = new Alarm();
                        iotAlarm.setName("HR");
                        iotAlarm.setValue(BigDecimal.valueOf(hrOne.getHr()));
                        iotAlarm.setSn(sn);
                        iotAlarm.setTimeStamp(hrOne.getTimeStamp());
                        try {
                            alarmMapper.insertAlarm(iotAlarm);
                        } catch (DuplicateKeyException ignored) {
                        }
                    }
                }

                List<Spo2Alarm> spo2AlarmList = one.getAlarmSpo2s();
                if (spo2AlarmList != null && !spo2AlarmList.isEmpty()) {
                    for (Spo2Alarm spo2One : spo2AlarmList) {
                        Alarm iotAlarm = new Alarm();
                        iotAlarm.setName("SPO2");
                        iotAlarm.setValue(BigDecimal.valueOf(spo2One.getSpo2()));
                        iotAlarm.setSn(sn);
                        iotAlarm.setTimeStamp(spo2One.getTimeStamp());
                        try {
                            alarmMapper.insertAlarm(iotAlarm);
                        } catch (DuplicateKeyException ignored) {
                        }
                    }
                }
            }
        }
        String key = String.format("HEALTH_WEARABLE_ALARM_%s", sn);
        redisCache.deleteObject(key);
    }
}
